from operation.HDFSUtil import HDFSSERVER
from operation.BaseUtil import LOG
import pandas as pd
import numpy as np
import json
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Instantiate DenseNet Operation
算子名称：DenseNet DenseNet
算子描述：DenseNet是康奈尔大学博士后黄高博士等人在2017年提出的，基于Dense Block这种内部相互连接的特点能够减轻梯度消失的现象、加强特征传递并在一定程度上减少了参数数量。
算子参数: 
0 : input_data_path 文件路径 图片数据文件路径
1 : model DenseNet模型 DenseNet模型选择
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from operation.pretrained.DenseNet import *
def denseNet(input_data_path, output_data_path, model):
    if model == 'null': model = None
    labels, image_paths = HDFSSERVER.load_images(input_data_path)
    ids = np.arange(len(image_paths))
    # 这里是为了和主平台的图片读取接口一致，22222端口对应tmp
    web_paths = [HDFSSERVER.web_path + "/" + i[5:] for i in image_paths]
    if model == "DenseNet121":
        preds, preds_prob = DenseNet121(img_paths=image_paths, labels=labels, return_prob=1)
    elif model == "DenseNet169":
        preds, preds_prob = DenseNet169(img_paths=image_paths, labels=labels, return_prob=1)
    elif model == "DenseNet201":
        preds, preds_prob = DenseNet169(img_paths=image_paths, labels=labels, return_prob=1)

    resultDF = pd.DataFrame([ids, web_paths, labels, preds, preds_prob]).T
    resultDF.columns = ["id", "图片", "标签", "预测", "概率"]
    schema = []
    for i in resultDF.columns:
        schema.append({'column_name' : i, 'column_type': 'float'})

    check_status = HDFSSERVER.save_data(output_data_path, resultDF, data_type="dataframe")
    if check_status == "success":
        LOG.info("DenseNet run success")
    return json.dumps({"type": "dataframe"})
